package com.kuangstudy.kpcode;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.servlet.KaptchaServlet;
import com.google.code.kaptcha.util.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 飞哥B站地址：https://space.bilibili.com/490711252
 * 记得关注和三连哦！
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2021/12/26 21:29
 */
@Configuration
@ConditionalOnClass(KaptchaServlet.class)
@EnableConfigurationProperties(KaptchaProperties.class)
public class KaptchaConfiguration {

    @Bean
    public Producer producer(KaptchaProperties kaptchaProperties) {
        Properties properties = new Properties();
        properties.setProperty("kaptcha.image.width", kaptchaProperties.getImageWidth() + "");
        properties.setProperty("kaptcha.image.height", kaptchaProperties.getImageHeight() + "");
        properties.setProperty("kaptcha.textproducer.font.color", kaptchaProperties.getTextColor() + "");
        properties.setProperty("kaptcha.border.color", "105,179,90");
        properties.setProperty("kaptcha.textproducer.font.size", kaptchaProperties.getTextFontSize() + "");
        properties.setProperty("kaptcha.session.key", kaptchaProperties.getSessionKey() + "");
        properties.setProperty("kaptcha.textproducer.char.length", kaptchaProperties.getTextLen() + "");
        properties.setProperty("kaptcha.textproducer.char.string", kaptchaProperties.getTextCharStr() + "");
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
}
